<template>
  <el-container>
    <el-main>
      <el-form :model="form.model" :rules="form.rules" ref="formRef" label-width="100px">
        <el-form-item label="用户头像" prop="avatar">
          <sa-uploader v-model="form.model.avatar" fileType="image"></sa-uploader>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.model.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickname">
          <el-input v-model="form.model.nickname" placeholder="请输入用户昵称"></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="form.model.email" placeholder="请输入电子邮箱"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.model.mobile" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="用户密码">
          <el-input v-model="form.model.password" placeholder="不修改请留空"></el-input>
        </el-form-item>
        <el-form-item label="用户性别">
          <el-radio-group v-model="form.model.gender">
            <el-radio :label="0">未知</el-radio>
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="创建时间" v-if="props.modal.params.type == 'edit'">
          {{ form.model.create_time }}
        </el-form-item>
        <el-form-item label="更新时间" v-if="props.modal.params.type == 'edit'">
          {{ form.model.update_time }}
        </el-form-item>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <el-button
        v-if="modal.params.type == 'add'"
        v-auth="'shop.admin.data.fakeuser.add'"
        type="primary"
        @click="confirm"
        >保存</el-button
      >
      <el-button
        v-if="modal.params.type == 'edit'"
        v-auth="'shop.admin.data.fakeuser.edit'"
        type="primary"
        @click="confirm"
        >更新</el-button
      >
    </el-footer>
  </el-container>
</template>
<script setup>
  import { onMounted, reactive, ref, unref } from 'vue';
  import { api } from '../data.service';
  import { cloneDeep } from 'lodash';

  const emit = defineEmits(['modalCallBack']);
  const props = defineProps({
    modal: {
      type: Object,
    },
  });
  // 添加 编辑 form
  let formRef = ref(null);
  const form = reactive({
    model: {
      avatar: '',
      username: '',
      nickname: '',
      email: '',
      mobile: '',
      password: '',
      gender: 0,
    },
    rules: {
      avatar: [{ required: true, message: '请上传头像', trigger: 'blur' }],
      username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
      nickname: [{ required: true, message: '请输入用户昵称', trigger: 'blur' }],
      email: [{ required: true, message: '请输入电子邮箱', trigger: 'blur' }],
      mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
    },
  });
  const loading = ref(false);
  // 获取详情
  async function getDetail(id) {
    loading.value = true;
    const { error, data } = await api.fakeUser.detail(id);
    if (error === 0) {
      form.model = data;
      form.model.password = '';
    }
    loading.value = false;
  }
  // 表单关闭时提交
  async function confirm() {
    // 表单验证
    unref(formRef).validate(async (valid) => {
      if (!valid) return;
      let submitForm = cloneDeep(form.model);
      const { error } =
        props.modal.params.type == 'add'
          ? await api.fakeUser.add(submitForm)
          : await api.fakeUser.edit(props.modal.params.id, submitForm);
      if (error == 0) {
        emit('modalCallBack', { event: 'confirm' });
      }
    });
  }
  async function init() {
    if (props.modal.params.id) {
      await getDetail(props.modal.params.id);
    }
  }
  onMounted(() => {
    init();
  });
</script>
